<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate"> 新增类型 </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
          />
        </template>
        <template #dictType="{ record }">
          <Tag color="green" @click="handleDetail({record})" style="cursor:pointer">
            {{ record.dictType }}
          </Tag>
        </template>
      </BasicTable>
      <DictTypeModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup name="Dict">
  import { ref } from 'vue'
  import { PageWrapper } from "@/components/Page";
  import { BasicTable, TableAction, useTable } from "@/components/Table";
  import { DeleteDictType, GetAllDictListType } from "@/api/system/dict";
  import { searchFormSchema,columns } from "./dict.data";
  import DictTypeModal  from "@/views/dict/DictTypeModal.vue";
  import { useModal } from "@/components/Modal";
  import { notification } from "ant-design-vue";
  import { Tag } from 'ant-design-vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { useGo } from '@/hooks/web/usePage';
  import { useDictStore } from "@/store/modules/dict";
  const { t } = useI18n();
  const go = useGo();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '字典类型列表',
    api: GetAllDictListType,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
  });
  function handleCreate() {
    openModal(true, { isUpdate: false });
  }
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  async function handleDelete(record) {
    console.log(record)
    //删除
    const res = await DeleteDictType(record.dictId)
    if(res.code == 200){
      notification.success({
        message: t('sys.delete.success'),
        description: t('sys.delete.success'),
        duration: 3,
      });
      //从store中删除
      useDictStore().delDict(record.dictType);
      await reload();
    }else{
      notification.error({
        message: t('sys.delete.fail'),
        description: res.msg,
        duration: 3,
      });
    }
  }
  function handleSuccess() {
    reload();
  }
  function handleSearch() {
    reload();
  }
  function handleDetail(record) {
    console.log(record)
    go(`/dictData/${record.record.dictType}`);
  }
</script>
<style lang="scss" scoped>

</style>
